package com.qf.test;

import com.qf.util.ZkClientUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

/**
 * @author Ypl
 * @version V1.0
 * @Project OctoberAndNovember
 * @Package com.qf.test
 * @Description:
 * @Date 2022/11/6 19:30
 */
@SuppressWarnings("all")
public class testListener {

    @Test
    public void testL() throws Exception {
        CuratorFramework zkClient = ZkClientUtil.getZkClient();

        //创建一个node对象，
        NodeCache nodeCache = new NodeCache(zkClient, "/ypl");
        nodeCache.start();
        //添加一个监听器
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                //节点数据
                byte[] data = nodeCache.getCurrentData().getData();
                //节点状态
                Stat stat = nodeCache.getCurrentData().getStat();
                //节点名称
                String path = nodeCache.getCurrentData().getPath();

                System.out.println("节点的数据是："+new String(data));
                System.out.println("节点的状态是："+stat);
                System.out.println("节点的名称是："+path);
            }
        });

        System.out.println("开始监听");

        //添加阻塞时方法，保证持续监听
        System.in.read();
    }
}
